<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="~assets/image/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="loginOut" type="info">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边区域 -->
      <el-aside :width="isCollapse ? '63px' : '200px'" >
        <!-- 折叠左侧菜单栏 -->
        <div class="toggle_button" @click="toggleCollapse">
          <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
        </div>
        <el-menu
          background-color="#313743"
          text-color="#fff"
          :collapse="isCollapse"
          :collapse-transition="false"
          unique-opened
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>

          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单栏数据
      menuList: [],
      iconObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao',
      },
      // 折叠左侧菜单
      isCollapse: false,
      // 被激活的链接
      activePath: ''
    }
  },
  created(){
    // 左侧菜单数据
    this.getMenuList(),
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut() {
      // 清空缓存中的 token
      window.sessionStorage.clear();
      // 重定向到 login 页面
      this.$router.push("/login");
    },
    // 请求获取左边菜单数据
    async getMenuList() {
      let {data: res} = await this.$http.get('menus')
      // 返回的状态码不是 200 则提示用户
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      // console.log(res.data);
    },
    // 点击按钮折叠左侧菜单栏
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 点击获取到被点击的 item 项的路径
    saveNavState(activePath) {
      // 把路径放进缓存中
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
};
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #313743;
  .el-menu {
    border-right: none;
  }
  .toggle_button {
    color: #fff;
    text-align: center;
    background-color: #485164;
    padding: 3px 0;
    font-size: 18px;
  }
  
  .iconfont {
    margin-right: 10px;
  }
}
.el-main {
  background-color: #e9edf1;
}
</style>